package com.pipayshop.controller;

import com.pipayshop.domain.ShopCommodity;
import com.pipayshop.domain.dto.ShopCommodityDto;
import com.pipayshop.domain.vo.CommodityDetailVO;
import com.pipayshop.domain.vo.ShopCommodityPageVo;
import com.pipayshop.entity.vo.ResponseVO;
import com.pipayshop.exception.BusinessException;
import com.pipayshop.service.OnlineStoreShopCommodityService;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.annotation.MultipartConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author xiaoqiu
 * @date 2024/9/10 11:06
 * @description 网店商品接口
 */
@Tag(name  = "网店商品接口",description  = "网店商品接口")
@RestController
@RequestMapping("/pipayshopapi/commodity")
@Slf4j

public class OnlineStoreShopCommodityController {

    @Autowired
    private OnlineStoreShopCommodityService shopCommodityService;

    @Value("${spring.servlet.multipart.max-file-size}")
    private String maxFileSize;

    @Value("${spring.servlet.multipart.max-request-size}")
    private String maxRequestSize;

    @ApiOperation(value = "展示网店商品")
    @PostMapping("/listCommodity")
    public ResponseVO listCommodity(@RequestBody ShopCommodityDto shopCommodityDto){
        ShopCommodityPageVo shopCommodityPageVo = shopCommodityService.listCommodity(shopCommodityDto);
        return ResponseVO.getSuccessResponseVo(shopCommodityPageVo);
    }

    @ApiOperation(value = "添加或更新网店商品")
    @PostMapping("/SaveOrUpdate")
    public ResponseVO<String> addCommodity(@RequestBody ShopCommodityDto shopCommodityDto) {
        try {
            // 调用服务层的保存或更新方法
            boolean result = shopCommodityService.saveOrUpdate(shopCommodityDto);


            if (!result) {
                // 如果保存或更新失败，抛出异常
                throw new BusinessException("add_failed");
            }

            // 返回成功响应
            return ResponseVO.getSuccessResponseVo("add_success");

        }catch (BusinessException e) {
            throw new BusinessException(e.getMessage());
        }catch (Exception e) {
            // 捕获其他未知异常并返回通用错误信息
            throw new BusinessException("add_failed");
        }
    }


    @ApiOperation(value = "商品图片的地址集合上传")
    @PostMapping("/shopImagsListUp")
    public ResponseVO shopImagsListUp(MultipartFile multipartFile){
            String imageId = shopCommodityService.shopImagsUp(multipartFile);
            if ("图片未通过审核".equals(imageId)){
                throw new BusinessException("image_not_approved");
            }
        return ResponseVO.getSuccessResponseVo(imageId);
    }
    @ApiOperation(value = "网店商品上传详情图片")
    @PostMapping("/itemDetailImagsUp")
    public ResponseVO<String> itemDetailImagsUp(MultipartFile multipartFile){
        String imageId = shopCommodityService.shopDetailImagsUp(multipartFile);
        if ("图片未通过审核".equals(imageId)){
            throw new BusinessException("image_not_approved");
        }
        return ResponseVO.getSuccessResponseVo(imageId);
    }

    @GetMapping("getInventoryByCommodityId/{CommodityId}")
    @ApiOperation("根据商品id获取商品库存")
    public ResponseVO getInventoryByCommodityId(@PathVariable("CommodityId")String commodityId){
        try {
            Integer Residue = shopCommodityService.getInventoryByCommodityId(commodityId);
            if (Residue == null) {
                throw new Exception();
            }
            return ResponseVO.getSuccessResponseVo(Residue);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("query_failed");
        }
    }

    @GetMapping("itemCommodityDetail/{commodityId}")
    @ApiOperation("获取网店商品详情接口")
    public ResponseVO<CommodityDetailVO> shopCommodityDetail(@PathVariable("commodityId") String commodityId) {
        try {

            CommodityDetailVO commodityDetailVO = shopCommodityService.itemCommodityDetail(commodityId);
            if (commodityDetailVO == null) {
                throw new Exception();
            }
            return ResponseVO.getSuccessResponseVo(commodityDetailVO);
        }catch (BusinessException e) {
            throw new BusinessException(e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new BusinessException("get_product_details_failed");
        }
    }


//    @GetMapping("collectList/{page}/{limit}/{userId}")
//    @ApiOperation("根据用户id查询用户收藏的商品列表")
//    public ResponseVO<PageVo> collectList(@PathVariable("page") Integer page,
//                                          @PathVariable("limit") Integer limit,
//                                          @PathVariable("userId") String userId) {
//        try {
//            PageVo collectList = shopCommodityService.getCollectList(page, limit, userId);
//            return ResponseVO.getSuccessResponseVo(collectList);
//        } catch (Exception e) {
//            log.error(e.getMessage());
//            throw new BusinessException("查询失败，请联系后台人员");
//        }
//    }


    @GetMapping("upOrdown/{commodityId}/{status}")
    @ApiOperation("上下架网店商品详情接口")
    public ResponseVO upOrdown(@PathVariable("commodityId") String commodityId,
                               @PathVariable("status") Integer status) {
        try {

            Integer commodityDetailVO = shopCommodityService.upOrdown(commodityId,status);
            if (commodityDetailVO == 0) {
                throw new Exception();
            }
            return ResponseVO.getSuccessResponseVo(commodityDetailVO);
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new BusinessException("on_off_shelf_failed");
        }
    }
    /**
     * 删除商品
     */
    @DeleteMapping("delCommodity/{commodityId}")
    @ApiOperation("删除商品接口")
    public ResponseVO delCommodity(@PathVariable("commodityId") String commodityId) {
           Integer commodityDetailVO = shopCommodityService.delCommodity(commodityId);
            if (commodityDetailVO == 0) {
               return ResponseVO.getFalseResponseMsg("delete_product_failed");
            }
            return ResponseVO.getSuccessResponseVo("delete_product_success");

    }


}
